package com.lwl.study.dao;


import com.lwl.study.config.DataSourceUtils;
import com.lwl.study.pojo.Account;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Component
public class AccountDao {


    public List<Account> findAll() throws SQLException {
        Connection connection = DataSourceUtils.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement("select * from account");
        ResultSet resultSet = preparedStatement.executeQuery();
        List<Account> accounts = new ArrayList<>();

        while(resultSet.next()) {
            Account account = new Account();
            account.setCardNo(resultSet.getString("cardNo"));
            account.setName(resultSet.getString("name"));
            account.setMoney(resultSet.getInt("money"));
            accounts.add(account);
        }
        resultSet.close();
        preparedStatement.close();
        connection.close();
        return accounts;
    }

}
